package ca.scotthyndman.as2haxe.dom;

import java.util.ArrayList;
import java.util.List;

import ca.scotthyndman.as2haxe.dom.visitor.IVisitor;

/**
 * Represents a conditional expression (eg, expression ? expression :
 * expression)
 */
public class ASConditionalExpression extends ASExpression {

	/**
	 * The "expression" structural property of this node type.
	 * 
	 * @since 3.0
	 */
	public static final ASChildPropertyDescriptor EXPRESSION_PROPERTY = new ASChildPropertyDescriptor(
			ASConditionalExpression.class, "expression", ASExpression.class,
			true);

	/**
	 * The "thenExpression" structural property of this node type.
	 * 
	 * @since 3.0
	 */
	public static final ASChildPropertyDescriptor THEN_EXPRESSION_PROPERTY = new ASChildPropertyDescriptor(
			ASConditionalExpression.class, "thenExpression",
			ASExpression.class, true);

	/**
	 * The "elseExpression" structural property of this node type.
	 * 
	 * @since 3.0
	 */
	public static final ASChildPropertyDescriptor ELSE_EXPRESSION_PROPERTY = new ASChildPropertyDescriptor(
			ASConditionalExpression.class, "elseExpression",
			ASExpression.class, true);

	/**
	 * A list of property descriptors (element type:
	 * {@link ASPropertyDescriptor}), or null if uninitialized.
	 */
	private static final List<ASPropertyDescriptor> PROPERTY_DESCRIPTORS;

	static {
		List<Object> properyList = new ArrayList<Object>(4);
		createPropertyList(ASConditionalExpression.class, properyList);
		addProperty(EXPRESSION_PROPERTY, properyList);
		addProperty(THEN_EXPRESSION_PROPERTY, properyList);
		addProperty(ELSE_EXPRESSION_PROPERTY, properyList);
		PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
	}

	/**
	 * Returns a list of structural property descriptors for this node type.
	 * Clients must not modify the result.
	 * 
	 * @return a list of property descriptors
	 */
	public static List propertyDescriptors() {
		return PROPERTY_DESCRIPTORS;
	}

	private ASExpression expression;

	private ASExpression thenExpression;

	private ASExpression elseExpression;

	/**
	 * Constructs a new instance of ASConditionalExpression
	 * 
	 * @param ast
	 *            The abstract syntax tree that owns this element
	 */
	public ASConditionalExpression(AST ast) {
		super(ast);
	}

	/**
	 * @param expression
	 *            the expression to set
	 */
	public void setExpression(ASExpression expression) {
		if (expression == null) {
			throw new IllegalArgumentException();
		}
		ASTNode oldChild = this.expression;
		preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
		this.expression = expression;
		postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
	}

	/**
	 * @return the expression
	 */
	public ASExpression getExpression() {
		return expression;
	}

	/**
	 * @param thenExpression
	 *            the thenExpression to set
	 */
	public void setThenExpression(ASExpression thenExpression) {
		if (expression == null) {
			throw new IllegalArgumentException();
		}
		ASTNode oldChild = this.thenExpression;
		preReplaceChild(oldChild, expression, THEN_EXPRESSION_PROPERTY);
		this.thenExpression = expression;
		postReplaceChild(oldChild, expression, THEN_EXPRESSION_PROPERTY);
	}

	/**
	 * @return the thenExpression
	 */
	public ASExpression getThenExpression() {
		return thenExpression;
	}

	/**
	 * @param elseExpression
	 *            the elseExpression to set
	 */
	public void setElseExpression(ASExpression elseExpression) {
		if (expression == null) {
			throw new IllegalArgumentException();
		}
		ASTNode oldChild = this.elseExpression;
		preReplaceChild(oldChild, expression, ELSE_EXPRESSION_PROPERTY);
		this.elseExpression = expression;
		postReplaceChild(oldChild, expression, ELSE_EXPRESSION_PROPERTY);
	}

	/**
	 * @return the elseExpression
	 */
	public ASExpression getElseExpression() {
		return elseExpression;
	}

	//
	// ======== INTERNAL GET SET
	//

	/*
	 * (omit javadoc for this method) Method declared on ASTNode.
	 */
	final ASTNode internalGetSetChildProperty(
			ASChildPropertyDescriptor property, boolean get, ASTNode child) {
		if (property == EXPRESSION_PROPERTY) {
			if (get) {
				return getExpression();
			} else {
				setExpression((ASExpression) child);
				return null;
			}
		}
		if (property == THEN_EXPRESSION_PROPERTY) {
			if (get) {
				return getThenExpression();
			} else {
				setThenExpression((ASExpression) child);
				return null;
			}
		}
		if (property == ELSE_EXPRESSION_PROPERTY) {
			if (get) {
				return getElseExpression();
			} else {
				setElseExpression((ASExpression) child);
				return null;
			}
		}
		// allow default implementation to flag the error
		return super.internalGetSetChildProperty(property, get, child);
	}

	// ===== TO STRING AND VISITOR

	@Override
	public String toString() {
		return getExpression().toString() + " ? "
				+ getThenExpression().toString() + " : "
				+ getElseExpression().toString();
	}

	@Override
	public void accept(IVisitor as2Visitor, boolean recursive) {
		recursive = as2Visitor.visit(this);

		if (!recursive) {
			return;
		}

		getExpression().accept(as2Visitor, recursive);
		getThenExpression().accept(as2Visitor, recursive);
		getElseExpression().accept(as2Visitor, recursive);
	}
}
